package com.zbkj.admin.task.mszs;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.common.constants.MszsConstants;
import com.zbkj.common.model.mszs.MszsOrder;
import com.zbkj.common.model.mszs.MszsRoom;
import com.zbkj.common.model.user.User;
import com.zbkj.common.model.user.UserIntegralRecord;
import com.zbkj.common.model.user.enums.UserIntegralRecordLinkTypeEnums;
import com.zbkj.common.model.user.enums.UserIntegralRecordStatusEnums;
import com.zbkj.common.model.user.enums.UserIntegralRecordTypeEnums;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.service.mszs.MszsOrderService;
import com.zbkj.service.service.mszs.MszsRoomService;
import com.zbkj.service.service.user.UserIntegralRecordService;
import com.zbkj.service.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 民宿助手-用户积分扣除task
 * @author lingfe
 */
@Slf4j
@Component
@Configuration //读取配置
@EnableScheduling // 2.开启定时任务
public class UserIntegralDeductTask {

    @Autowired
    private UserService userService;

    @Autowired
    private UserIntegralRecordService userIntegralRecordService;

    @Scheduled(cron = "0 0 12 * * ?") // 每天12点执行
    //@Scheduled(fixedDelay = 1000 * 20L) //20s-同步一次数据
    public void init(){
        log.info("---用户积分扣除task: Execution Time - {}", DateUtil.nowDateTime());
        try {
            //定义查询对象
            LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.gt(User::getIntegral,0);
            List<User> users = userService.list(lambdaQueryWrapper);

            //定义更新记录集合
            List<User> updatedUsers = new ArrayList<>();
            List<UserIntegralRecord> userIntegralRecordList = new ArrayList<>();

            //循环处理
            users.forEach(item->{
                //扣除积分
                int integral = item.getIntegral();
                integral --;
                item.setIntegral(integral);
                updatedUsers.add(item);

                //用户积分记录
                UserIntegralRecord  record = new UserIntegralRecord();
                record.setUid(item.getUid());
                record.setIntegral(1);
                record.setBalance(item.getIntegral());
                record.setStatus(UserIntegralRecordStatusEnums.complete.getStatus());
                record.setLinkType(UserIntegralRecordLinkTypeEnums.deduction_day.name());
                record.setLinkId("0");
                record.setType(UserIntegralRecordTypeEnums.sub.getTypeValue());
                record.setTitle(UserIntegralRecordLinkTypeEnums.deduction_day.getLinkTypeName());
                record.setMark(StrUtil.format("{}，{}{}个积分,剩余{}个积分。",
                        record.getTitle(),
                        UserIntegralRecordTypeEnums.sub.getTypeName(),
                        record.getIntegral(),
                        record.getBalance()));
                userIntegralRecordList.add(record);
            });

            //批量更新
            userService.updateBatchById(updatedUsers);
            userIntegralRecordService.saveBatch(userIntegralRecordList);
        }catch (Exception e){
            log.error("---用户积分扣除task-发生错误:" + " | msg : {}", e.getMessage());
            e.printStackTrace();
        }
    }
}
